/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TambahKaryawanGUI.java
 *
 * Created on 25 Jul 12, 7:54:39
 */
package gui;

import dao.GolonganDAO;
import dao.KaryawanDAO;
import entity.Karyawan;
import javax.persistence.EntityManager;
import javax.swing.JOptionPane;

/**
 *
 * @author Dhurand
 */
public class UbahKaryawanGUI extends javax.swing.JInternalFrame {

    EntityManager em;
    KaryawanDAO dao;
    
    /** Creates new form TambahKaryawanGUI */
    public UbahKaryawanGUI(EntityManager em) {
        initComponents();
        this.em = em;
        this.bindingKode();
        this.bindingGolongan();
        //pria_rb.setSelected(true);
        //wanita_rb.setSelected(false);
        dao = new KaryawanDAO(em);
    }

    public void bindingKode(){
        KaryawanDAO dao = new KaryawanDAO(em);
        nik_cmb.removeAllItems();
        for (int i = 0; i < dao.getAll().size(); i++) {
            nik_cmb.addItem(dao.getAll().get(i).getNIK());
            
        }
    }
    
    public void bindingGolongan() {
        GolonganDAO dao = new GolonganDAO(em);
        golongan_cmb.removeAllItems();
        for (int i = 0; i < dao.getAll().size(); i++) {

            golongan_cmb.addItem(dao.getAll().get(i).getKodeGolongan());

        }
    }
    
    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        nama_txt = new javax.swing.JTextField();
        golongan_cmb = new javax.swing.JComboBox();
        pria_rb = new javax.swing.JRadioButton();
        wanita_rb = new javax.swing.JRadioButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        alamat_txt = new javax.swing.JTextArea();
        ubah_btn = new javax.swing.JButton();
        reset_btn = new javax.swing.JButton();
        keluar_btn = new javax.swing.JButton();
        nik_cmb = new javax.swing.JComboBox();

        setTitle("Ubah Karyawan");

        jLabel1.setText("NIK");

        jLabel2.setText("Nama");

        jLabel3.setText("Golongan");

        jLabel4.setText("Gender");

        jLabel5.setText("Alamat");

        pria_rb.setText("Pria");
        pria_rb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                pria_rbActionPerformed(evt);
            }
        });

        wanita_rb.setText("Wanita");
        wanita_rb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wanita_rbActionPerformed(evt);
            }
        });

        alamat_txt.setColumns(20);
        alamat_txt.setRows(5);
        jScrollPane1.setViewportView(alamat_txt);

        ubah_btn.setText("Ubah");
        ubah_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ubah_btnActionPerformed(evt);
            }
        });

        reset_btn.setText("Reset");
        reset_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reset_btnActionPerformed(evt);
            }
        });

        keluar_btn.setText("Keluar");
        keluar_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                keluar_btnActionPerformed(evt);
            }
        });

        nik_cmb.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nik_cmbActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ubah_btn)
                        .addGap(18, 18, 18)
                        .addComponent(reset_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(keluar_btn, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pria_rb)
                        .addGap(18, 18, 18)
                        .addComponent(wanita_rb))
                    .addComponent(nama_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                    .addComponent(golongan_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE)
                    .addComponent(nik_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(nik_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(nama_txt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(golongan_cmb, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(pria_rb)
                    .addComponent(wanita_rb))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ubah_btn)
                    .addComponent(reset_btn)
                    .addComponent(keluar_btn))
                .addContainerGap(31, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void keluar_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_keluar_btnActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_keluar_btnActionPerformed

    private void pria_rbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pria_rbActionPerformed
        // TODO add your handling code here:
        if (pria_rb.isSelected()) {
            wanita_rb.setSelected(false);
        }
    }//GEN-LAST:event_pria_rbActionPerformed

    private void wanita_rbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wanita_rbActionPerformed
        // TODO add your handling code here:
        if (wanita_rb.isSelected()) {
            pria_rb.setSelected(false);
        }
    }//GEN-LAST:event_wanita_rbActionPerformed

    private void reset_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reset_btnActionPerformed
        // TODO add your handling code here:
        //nik_txt.setText("");
        nama_txt.setText("");
        this.bindingGolongan();
        //this.bindingKode();
        //this.bindingKode();
        pria_rb.setSelected(true);
        wanita_rb.setSelected(false);
        alamat_txt.setText("");
        //cek_txt.setText("-----");
    }//GEN-LAST:event_reset_btnActionPerformed

    private void ubah_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ubah_btnActionPerformed
        // TODO add your handling code here:
        KaryawanDAO dao = new KaryawanDAO(em);
        Karyawan k = new Karyawan();
        String message = "";
        if ( nama_txt.getText().isEmpty()
                || alamat_txt.getText().isEmpty()) {
            message = "Data Harus Diisi";
            JOptionPane.showMessageDialog(this, message);
        }  else {
            
                try {
                    String gender = "";
                    //k.setNIK(nik_txt.getText());
                    k.setNama(nama_txt.getText());
                    k.setGolongan(golongan_cmb.getSelectedItem().toString());
                    if (pria_rb.isSelected()) {
                        gender = "Pria";
                    }
                    if (wanita_rb.isSelected()) {
                        gender = "Wanita";
                    }
                    k.setGender(gender);
                    k.setAlamat(alamat_txt.getText());

                    JOptionPane.showMessageDialog(this, dao.Update(nik_cmb.getSelectedItem().toString(), k));
                    
                } catch (Exception e) {
                    JOptionPane.showMessageDialog(this, e.getMessage());
                }
            

        }

    }//GEN-LAST:event_ubah_btnActionPerformed

    private void nik_cmbActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nik_cmbActionPerformed
        // TODO add your handling code here:
        KaryawanDAO dao = new KaryawanDAO(em);
        nama_txt.setText(dao.getByNIK(nik_cmb.getSelectedItem().toString()).get(0).getNama());
        
        golongan_cmb.setSelectedItem(dao.getByNIK(nik_cmb.getSelectedItem().toString()).get(0).getGolongan());
        
        String gender = dao.getByNIK(nik_cmb.getSelectedItem().toString()).get(0).getGender();
        
        if(gender.equalsIgnoreCase("Pria")){
            pria_rb.setSelected(true);
            wanita_rb.setSelected(false);
        }else if(gender.equalsIgnoreCase("Wanita")){
            pria_rb.setSelected(false);
            wanita_rb.setSelected(true);
        }
        
        alamat_txt.setText(dao.getByNIK(nik_cmb.getSelectedItem().toString()).get(0).getAlamat());
    }//GEN-LAST:event_nik_cmbActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea alamat_txt;
    private javax.swing.JComboBox golongan_cmb;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton keluar_btn;
    private javax.swing.JTextField nama_txt;
    private javax.swing.JComboBox nik_cmb;
    private javax.swing.JRadioButton pria_rb;
    private javax.swing.JButton reset_btn;
    private javax.swing.JButton ubah_btn;
    private javax.swing.JRadioButton wanita_rb;
    // End of variables declaration//GEN-END:variables
}
